﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.WinForms;
using RM2012Server.Controller;
using RM2012Server.Business;
using RM2012Server.Model;

namespace RM2012Server.View
{
    public partial class frmCategory : Office2007Form
    {
        #region Fields
        CategoryCtrl m_CategoryCtrl = new CategoryCtrl();
        public static bool isNew = false;
        #endregion

        #region Constructor
        public frmCategory()
        {
            InitializeComponent();
        }
        #endregion
        
        #region Load
        private void frmCategory_Load(object sender, EventArgs e)
        {
            isNew = false;
            m_CategoryCtrl.Show(dgvCate, bindingNavigatorCate, txtCateId, txtCateName, txtRemark);
            SaveItem.Enabled = false;
        }
        #endregion

        #region Button Click
        private void btnDongY_Click(object sender, EventArgs e)
        {
            CategoryInfo info = new CategoryInfo(txtCateId.Text, txtCateName.Text, txtRemark.Text);
            if (isNew == true)
            {
                m_CategoryCtrl.Add(info);
            } 
            else
            {
                m_CategoryCtrl.Update(info);
            }
            m_CategoryCtrl.Show(dgvCate, bindingNavigatorCate, txtCateId, txtCateName, txtRemark);
            SaveItem.Enabled = false;
            isNew = false;
        }

        private void btnLamLai_Click(object sender, EventArgs e)
        {
            isNew = false;
            m_CategoryCtrl.Show(dgvCate, bindingNavigatorCate, txtCateId, txtCateName, txtRemark);
            SaveItem.Enabled = false;
        }
        #endregion

        #region BindingNavigator Item Click
        private void AddNewItem_Click(object sender, EventArgs e)
        {
            isNew = true;
            if (dgvCate.Rows.Count == 0)
            {
                DeleteItem.Enabled = false;
            }
            DataRow m_Row = m_CategoryCtrl.AddNewRow();
            m_Row["CateId"] = GetID.Cate(dgvCate);
            m_Row["CateName"] = "";
            m_Row["Remark"] = "";
            m_CategoryCtrl.AddNewCate(m_Row);
            bindingNavigatorCate.BindingSource.MoveLast();
            SaveItem.Enabled = true;
        }

        private void RefreshItem_Click(object sender, EventArgs e)
        {
            isNew = false;
            m_CategoryCtrl.Show(dgvCate, bindingNavigatorCate, txtCateId, txtCateName, txtRemark);
            SaveItem.Enabled = false;
        }

        private void DeleteItem_Click(object sender, EventArgs e)
        {
            isNew = false;
            if (dgvCate.Rows.Count == 0)
            {
                DeleteItem.Enabled = false;
            }
            else if (dgvCate.SelectedRows.Count > 0)
            {
                if (MessageBoxEx.Show("Bạn có chắc chắn xóa?", "XÓA", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    foreach (DataGridViewRow row in dgvCate.SelectedRows)
                    {
                        bindingNavigatorCate.BindingSource.RemoveAt(row.Index);
                    }
                    SaveItem.Enabled = true;
                }
            }
        }

        private void SaveItem_Click(object sender, EventArgs e)
        {
            isNew = false;
            bindingNavigatorPositionItem.Focus();
            bool check = true;
            foreach (DataGridViewRow row in dgvCate.Rows)
            {
                if (row.Cells["colCateId"].Value.ToString() == "" || row.Cells["colCateName"].Value.ToString() == "")
                {
                    check = false;
                }
            }
            if (check)
            {
                if (!m_CategoryCtrl.SaveData())
                {
                    MessageBoxEx.Show("Không thể thực hiện !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    SaveItem.Enabled = false;
                }
            }
            else
            {
                MessageBoxEx.Show("Dữ liệu không hợp lệ. Mã và tên danh mục không được để trống !", "HÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void ExitItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
        
        #region Searching
        private void rbCateId_CheckedChanged(object sender, EventArgs e)
        {
            lblKeyWord.Text = "Mã danh mục :";
        }

        private void rbCateName_CheckedChanged(object sender, EventArgs e)
        {
            lblKeyWord.Text = "Tên danh mục :";
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                if (rbCateId.Checked == true)
                {
                    dt = m_CategoryCtrl.GetByLikeId(txtKeyWord.Text);
                }
                else if (rbCateName.Checked == true)
                {
                    dt = m_CategoryCtrl.GetByName(txtKeyWord.Text);
                }
                else
                {
                    dt = m_CategoryCtrl.GetByName(txtKeyWord.Text);
                }
                m_CategoryCtrl.ShowWithSources(dgvCate, bindingNavigatorCate, txtCateId, txtCateName, txtRemark, dt);
                SaveItem.Enabled = false;
            }
            catch
            {
                MessageBoxEx.Show("Không tìm thấy kết quả !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        #endregion
        
        #region DataGridView Event
        private void dgvCate_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            SaveItem.Enabled = true;
        }
        #endregion

        #region Context Menu Event Click
        private void AddNewItemContext_Click(object sender, EventArgs e)
        {
            AddNewItem_Click(sender, e);
        }

        private void RefreshItemContext_Click(object sender, EventArgs e)
        {
            RefreshItem_Click(sender, e);
        }

        private void DeleteItemContext_Click(object sender, EventArgs e)
        {
            DeleteItem_Click(sender, e);
        }

        private void SaveItemContext_Click(object sender, EventArgs e)
        {
            SaveItem_Click(sender, e);
        }

        private void ExitItemContext_Click(object sender, EventArgs e)
        {
            ExitItem_Click(sender, e);
        }
        #endregion
    }
}
